package com.bocweb.fly.jiaodao;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bocweb.fly.jiaodao.fragment.FmBusiness;
import com.bocweb.fly.jiaodao.fragment.FmFx;
import com.bocweb.fly.jiaodao.fragment.FmMsg;
import com.bocweb.fly.jiaodao.fragment.FmMy;
import com.github.mzule.activityrouter.annotation.Router;
import com.milk.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by fly on 2016/11/2.
 */
@Router("main")
public class MainAct extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.lin_fx)
    LinearLayout linFx;
    @Bind(R.id.lin_sq)
    LinearLayout linSq;
    @Bind(R.id.lin_msg)
    LinearLayout linMsg;
    @Bind(R.id.lin_my)
    LinearLayout linMy;

    @Bind(R.id.iv_fx)
    ImageView ivFx;
    @Bind(R.id.iv_sq)
    ImageView ivSq;
    @Bind(R.id.iv_msg)
    ImageView ivMsg;
    @Bind(R.id.iv_my)
    ImageView ivMy;

    @Bind(R.id.tv_fx)
    TextView tvFx;
    @Bind(R.id.tv_sq)
    TextView tvSq;
    @Bind(R.id.tv_msg)
    TextView tvMsg;
    @Bind(R.id.tv_my)
    TextView tvMy;

    @Bind(R.id.image)
    ImageView img;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        setSelect(0);
    }

    @OnClick({R.id.lin_sq, R.id.lin_fx, R.id.lin_msg, R.id.lin_my,R.id.image})
    public void onClick(View view) {
        resetImg();
        switch (view.getId()) {
            case R.id.lin_sq:
                setSelect(1);
                break;
            case R.id.lin_fx:
                setSelect(0);
                break;
            case R.id.lin_msg:
                setSelect(2);
                break;
            case R.id.lin_my:
                setSelect(3);
                break;
            case R.id.image:
                showPopupWindow(tvMy);
                break;

        }
    }

    public void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                if (mTab01 == null) {
                    mTab01 = new FmFx();
                    transaction.add(R.id.container, mTab01);
                } else {
                    transaction.show(mTab01);
                }
                ivFx.setImageResource(R.mipmap.tab_fx_check);
                tvFx.setTextColor(getResources().getColor(R.color.tv_click));
                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new FmBusiness();
                    transaction.add(R.id.container, mTab02);
                } else {
                    transaction.show(mTab02);
                }
                ivSq.setImageResource(R.mipmap.tab_sq_check);
                tvSq.setTextColor(getResources().getColor(R.color.tv_click));
                break;
            case 2:
                if (mTab03 == null) {
                    mTab03 = new FmMsg();
                    transaction.add(R.id.container, mTab03);
                } else {
                    transaction.show(mTab03);
                }
                ivMsg.setImageResource(R.mipmap.tab_msg_check);
                tvMsg.setTextColor(getResources().getColor(R.color.tv_click));
                break;
            case 3:
                if (mTab04 == null) {
                    mTab04 = new FmMy();
                    transaction.add(R.id.container, mTab04);
                } else {
                    transaction.show(mTab04);
                }
                ivMy.setImageResource(R.mipmap.tab_my_check);
                tvMy.setTextColor(getResources().getColor(R.color.tv_click));
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null) {
            transaction.hide(mTab01);
        }
        if (mTab02 != null) {
            transaction.hide(mTab02);
        }
        if (mTab03 != null) {
            transaction.hide(mTab03);
        }
        if (mTab04 != null) {
            transaction.hide(mTab04);
        }
    }

    private void resetImg() {
        ivFx.setImageResource(R.mipmap.tab_fx);
        ivSq.setImageResource(R.mipmap.tab_sq);
        ivMsg.setImageResource(R.mipmap.tab_msg);
        ivMy.setImageResource(R.mipmap.tab_my);

        tvFx.setTextColor(getResources().getColor(R.color.tv));
        tvSq.setTextColor(getResources().getColor(R.color.tv));
        tvMsg.setTextColor(getResources().getColor(R.color.tv));
        tvMy.setTextColor(getResources().getColor(R.color.tv));

    }
    PopupWindow popupWindow;
    private void showPopupWindow(View view) {
        // 一个自定义的布局，作为显示的内容
        final View contentView = LayoutInflater.from(MainAct.this).inflate(
                R.layout.publish_pop, null);
        RelativeLayout rel_bottom= (RelativeLayout) contentView.findViewById(R.id.rel_bottom);
        rel_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        popupWindow = new PopupWindow(contentView,
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        // popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);

    }


}
